package org.ithang.application.btAddr;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.ithang.application.btAddr.bean.BtAddr;
import org.ithang.application.btAddr.service.BtAddrService;
import org.ithang.application.currency.bean.Currency;
import org.ithang.application.currency.service.CurrencyService;
import org.ithang.application.customer.bean.Customer;
import org.ithang.application.customer.service.CustomerService;
import org.ithang.tools.model.Action;
import org.ithang.tools.model.ActionResult;
import org.ithang.tools.model.ActionValues;
import org.ithang.tools.model.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 提币地址管理
 * @author zyt 控制器
 *
 */
@Controller
@RequestMapping("/app/btAddr")
public class BtAddrAction extends Action<BtAddr>{

    @Autowired
    private BtAddrService btAddrService; 

    @Autowired
    private CustomerService customerService;
    
    @Autowired
    private CurrencyService currencyService;

    @ResponseBody
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ActionResult add(BtAddr btAddr){
		btAddrService.add(btAddr);
		return success();
	}
	
	 @RequestMapping("form")
    public String form(@RequestParam(value="id",required=false)Integer id,
    		           @RequestParam(value="customer_id",required=false,defaultValue="0")Integer customer_id,
    		           @RequestParam(value="currency_id",required=false,defaultValue="0")Integer currency_id,
    		           Model m){
    	if(null!=id){
    		BtAddr bean=btAddrService.get(id);
    		m.addAttribute(bean);
    	}else{
    		BtAddr bta=new BtAddr();
    		if(null!=customer_id&&customer_id>0){
    			Customer user=customerService.get(customer_id);
    			if(null!=user){
    				bta.setCustomer_id(user.getId());
        			bta.setTitle(user.getTitle());
        			bta.setMobile(user.getMobile());
    			}
    		}
    		if(null!=currency_id&&currency_id>0){
    			Currency cy=currencyService.get(currency_id);
    			if(null!=cy){
    				bta.setCurrency_id(currency_id);
    				bta.setCurrency_title(cy.getTitle());
    			}
    		}
    		m.addAttribute(bta);
    	}
    	return "app/btAddr/form";
    }
	
	
	@ResponseBody
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ActionResult update(BtAddr btAddr){
		btAddrService.update(btAddr);
		return success();
	}


    @ResponseBody
	@RequestMapping(value="get",method=RequestMethod.GET)
	public ActionResult get(Integer id){
		BtAddr r=btAddrService.get(id);
		return success(r);
	}
	
	@ResponseBody
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public ActionResult delete(@RequestParam(value="id",required=false)Integer id,@RequestParam(value="ids",required=false)String ids){
		
		if(null!=id&&id>0){
			btAddrService.delete(id);	
		}
		
		if(null!=ids&&ids.length()>0){
			btAddrService.batchDelete(ids.split(","));
		}
		
		return success();
	}
	
	@ResponseBody
	@RequestMapping(value="list",method={RequestMethod.GET,RequestMethod.POST})
	public ActionResult list(Integer... ids){
		return success(btAddrService.list(ids));
	}
	
	@ResponseBody
	@RequestMapping(value="page",method=RequestMethod.POST)
	public ActionResult page(BtAddr btAddr,Pager<BtAddr> page){
	    page.setBean(btAddr);
	    List<BtAddr> data=btAddrService.page(page);
	    page.setData(data);
		return success(page);
	}
	
	@ResponseBody
	@RequestMapping(value="query",method=RequestMethod.POST)
	public ActionResult query(HttpServletRequest request){
		ActionValues values=new ActionValues(request);
		return success(btAddrService.query(values));
	}
     

}